<template>
	<view class="content">
		<view class="user-top">
			<view class="top-tool">
				<navigator url="../newDriver/newDriver" hover-class="none">
					<image src="../../static/common/tool-msg.png" class="tool-msg"></image>
				</navigator>
				<navigator url="../user/setting" hover-class="none">
					<image src="../../static/common/tool-seting.png" class="tool-seting"></image>
				</navigator>
			</view>
			<view class="user-info">
				<navigator url="../user/user-info">
					<image src="../../static/common/useravatar.png" class="user-avator" v-if="!apptoken"></image>
					<image v-else :src="headimg?getImgUrl(headimg):'../../static/common/default-heading'" class="user-avator"></image>
					<!-- <image  src="../../static/common/default-heading.png" class="user-avator"></image>-->
				</navigator>
				
				<view class="user-info-block" v-if="apptoken">
					<view class="nick-name">{{nickName?nickName:"熊猫宝宝"}}</view>
					<view class="user-id">ID：{{id?id:"XM_NEF"}}</view>
				</view>
			</view>
			<!-- <image src="../../static/common/earn-money-me.png" class="earn-money"></image> -->
			<navigator url="../user/user-info" hover-class="none">
				<view class="userInfo">
					<image src="../../static/common/edit.png" class="edit-info"></image>
					个人资料
				</view>
			</navigator>
		</view>
		
		<view class="user-bottom">
			<view class="user-baseInfo">
				<navigator url="../user/captial-mingx" hover-class="none">
					<view class="baseInfo-item">
						<view class="capital-title">资金明细</view>
						<view class="capital-num">{{gulijin?gulijin:0}}</view>
					</view>
				</navigator>
				<!-- <navigator url="../user/share-panda" hover-class="none">
					<view class="baseInfo-item">
						<view class="capital-title">好友贡献鼓励金</view>
						<view class="capital-num">1080.50</view>
					</view>
				</navigator> -->
				<navigator url="../myFans/my-fans" hover-class="none">
					<view class="baseInfo-item">
						<view class="capital-title">好友数</view>
						<view class="capital-num">{{friend?friend:0}}</view>
					</view>
				</navigator>
			</view>
			
			<view class="user-order" @tap="goOrder">
				<view class="order-title">我的订单</view>
				<image src="../../static/common/question-icon.png" class="question-icon"></image>
			</view>
			<view class="user-order" @tap="update">
				<view class="order-title">软件更新</view>
				<image src="../../static/common/question-icon.png" class="question-icon"></image>
			</view>
			
			<view class="tab-block">
				<!-- <view class="tab-item" @tap="goShopCar">
					<image class="tab-icon" src="../../static/common/me-01.png"></image>
					<view class="tab-text">购物车</view>
				</view> -->
				<view class="tab-item" @tap="goCollection">
					<image class="tab-icon" src="../../static/common/me-02.png"></image>
					<view class="tab-text">收藏宝贝</view>
				</view>
				<view class="tab-item" @tap="orderBack">
					<image class="tab-icon" src="../../static/common/me-03.png"></image>
					<view class="tab-text">订单补录</view>
				</view>
				<view class="tab-item" @tap="goBrowseFootprints">
					<image class="tab-icon" src="../../static/common/me-04.png"></image>
					<view class="tab-text">浏览足迹</view>
				</view>
				<view class="tab-item" @tap="bindPhone">
					<image class="tab-icon" src="../../static/common/bindPhone.png"></image>
					<view class="tab-text">绑定手机号</view>
				</view>
			</view>
			
			<view class="nav-block">
				<view class="nav-item" @tap="goSharePanda">
					<image src="../../static/common/nav-01.png" class="nav-icon"></image>
					<view class="nav-text">分享赚钱</view>
				</view>
				<view class="nav-item" @tap="goSharePoster">
					<image src="../../static/common/nav-02.png" class="nav-icon"></image>
					<view class="nav-text">专属海报</view>
				</view>
				<!-- <view class="nav-item">
					<image src="../../static/common/nav-03.png" class="nav-icon"></image>
					<view class="nav-text">专属链接</view>
				</view> -->
				<view class="nav-item" @tap="goEncourageMoney">
					<image src="../../static/common/nav-04.png" class="nav-icon"></image>
					<view class="nav-text">我的鼓励金</view>
				</view>
				<view class="nav-item" @tap="goCash">
					<image src="../../static/common/nav-05.png" class="nav-icon"></image>
					<view class="nav-text">提现</view>
				</view>
				<view class="nav-item" @tap="AutoTaobao">
					<image src="../../static/common/nav-07.png" class="nav-icon"></image>
					<view class="nav-text">淘宝授权</view>
				</view>
				<view class="nav-item" @tap="goNewDriver">
					<image src="../../static/common/nav-06.png" class="nav-icon"></image>
					<view class="nav-text">帮助中心</view>
				</view>
				<view class="nav-item" @tap="goKeFu">
					<image src="../../static/common/nav-08.png" class="nav-icon"></image>
					<view class="nav-text">联系客服</view>
				</view>
			</view>
			
		</view>
		
		
		
	</view>
</template>

<script>
	const plug= uni.requireNativePlugin('UZK-Alibcsdk');
	//const Alibcsdk = uni.requireNativePlugin('UZK-Alibcsdk');
	import { myMixins } from '../../common/mixins.js'
	
	export default{
		components:{},
		mixins:[ myMixins ],
		data(){
			return{
					type: 'center',
					apptoken:"",
					userId:"",
					access_token:"",
					userInfo:"",
					headimg:"",
					id:"",
					nickName:"",
					gulijin:"",
					friend:"",
					OauthInfo:"",//淘宝授权信息
					
			}
		},
		onLoad() {
			this.apptoken = uni.getStorageSync("apptoken");
			this.userId = uni.getStorageSync("userId");
			
			//获取用户信息
			if(uni.getStorageSync('apptoken')){
				this.getUserInfo();
			}
			
		},
		onShow() {
			this.getUserInfo();
			//if(!this.checkLoginStatus()){
			if(!uni.getStorageSync('apptoken')){
				
				uni.navigateTo({
					url:'../login/login?type=1',
					animationType:'slide-in-bottom',
				})
				return;
			}
			
		},
		methods:{
			
			//淘宝授权
			
			AutoTaobao(){
				console.log(this.id)
				const _this = this;
				console.log(uni.getStorageSync("isAutoTbao"))
				if(uni.getStorageSync("isAutoTbao")){
					_this.$CommonJS.showToast("您已授权，无需重复授权");
					return;
				}
				//初始化
				plug.init(result=>{
				 if(result.status){
					 console.log("初始化成功");
					 //拉起手淘授权
					  plug.login(res=>{
							console.log(res)
							if(res.status){

								//登录成功
								_this.OauthInfo =  res.data;
								
								//拉起淘客静默授权
								plug.qdByhide({
								  url:"https://oauth.taobao.com/authorize?response_type=code&client_id=28869937&redirect_uri=https://pandayoupin.com/panda.htm&state=1212&view=wap",
									
								},result=> {
									console.log(result)
									uni.request({
										url:_this.$API.URL + _this.$API.getRelation_id,
										data:{
											code:result.data.code,
											state:result.data.state,
											apptoken:_this.apptoken,
											userId:	_this.id
										},
										success(res) {
											console.log(res)
											if(res.data.success){
												uni.setStorage({
													key:"isAutoTbao",
													data:true,
													success() {
														uni.showToast({
															title:"授权成功"
														})
													}
												})
												
											}else{
												uni.showModal({
													title:"淘宝授权失败",
													content:res.data.msg
												})
											}
										},
										fail(res) {
											uni.showToast({
												title:"授权失败"
											})
										}
									})
								});
							}else{
								_this.$CommonJS.showToast(res.msg)
							}
					  });  
				 }else{
					 console.log("初始化失败");
					 _this.$CommonJS.showToast(result.msg)
				 }
				});  
			},
			
			
			
			update(){
				//版本更新
				uni.showToast({
					title:"检查版本中..",
					icon:'loading'
				})
				var _this = this;
				var apptoken = uni.getStorageSync("apptoken");
				console.log(apptoken)
				var reHostP = "https://www.pandayoupin.com/img";
				var wgtVer = "";
				var newver = 0;
				var vercount = 0;
				//版本更新
				if(apptoken){
					//#ifdef APP-PLUS
						// 获取本地应用资源版本号
						//console.log(66666)
						plus.runtime.getProperty(plus.runtime.appid, function(inf) {
							console.log(inf)
							wgtVer = inf.version;
							if(wgtVer) {
								console.log("当前应用版本：" + wgtVer);
								
								if(plus.os.name == "Android") {
									var device = 1;
								} else if(plus.os.name == "iOS") {
									var device = 2;
								}
								uni.request({
									url:_this.$API.URL + _this.$API.appversion,
									data: {
										apptoken:apptoken,
										device:device,
										oldVersion:wgtVer
									},  
									success: (res) => {
										uni.hideToast();
										console.log(res)
										if(res.data.success){
											var d = res.data.data,
											m = d.length;
											newver = m;
											if(m>0){
												for(var i = 0;i < m; i++){
													//整包更新
													if(d[i].vtype == 1 || d[i].vtype == 2) {
														uni.showModal({
															title:"版本更新",
															content:"发现新版本,是否前往下载,下载前请先卸载旧版本",
															success(res) {
																if(res.confirm){
																	plus.runtime.openURL("https://www.pandayoupin.com");
																}
																
															}
														})
														return false;
													}
													//差量更新
													//var wgtUrl="http://demo.dcloud.net.cn/test/update/H5EF3C469.wgt";  
													var wgtUrl = d[i].url;
													console.log(wgtUrl)
													plus.downloader.createDownload(reHostP + '/' + wgtUrl, {
														filename: "_doc/update/"
													}, function(d, status) {
														console.log(d)
														if(status == 200) {
															uni.showLoading({
																		title:"版本更新中..."
																	});
															//installWgt(d.filename, i);
															plus.runtime.install(d.filename, {
																force: true
															}, function() {
																vercount++;
																								
																if(vercount == newver) {
																	uni.hideLoading();
																}
																plus.runtime.restart();  
															}, function(e) {
																uni.hideLoading();
																console.log(JSON.stringify(e));
															});
														
														} else {
															//uni.hideLoading();
														}
														
													}).start();
												}
											}else{
													uni.hideToast();
												_this.$CommonJS.showToast("已是最新版本,无需更新")
											}
										}
									}
								}); 
							} else {
								console.log('获取版本号失败');
								uni.hideToast();
								_this.$CommonJS.showToast("获取版本号失败")
							}
						
						});
					
						
					//#endif
				}else{
					  uni.hideToast();
						_this.$CommonJS.showToast("未登录")
				}
			},
			goOrder(){
				//我的订单
				uni.switchTab({
					url:'../order/order',
				})
			},
			getImgUrl(url){
				if(url.indexOf('http')==-1){
					return this.$CommonJS.baseurl + url;
				}else{
					return url;
				}
				
			},
			getUserInfo(){
				//获取用户信息
				//console.log(111)
				const _this = this;
				this.apptoken = uni.getStorageSync('apptoken');
			  //const userInfo = uni.getStorageSync('UserInfo');
				//console.log(userInfo)
				// this.headimg = userInfo.headimg;
				// this.id = userInfo.id || userInfo.userId;
				// this.nickName = userInfo.nickName;
				// this.gulijin = userInfo.gulijin;
				// this.friend = userInfo.friend;
				// uni.setStorage({
				// 	key:"gulijin",
				// 	data:userInfo.gulijin
				// })
				
				
				uni.request({
					url:this.$API.URL + this.$API.userView,
					data:{
						apptoken:this.apptoken
					},
					success(res) {
							console.log(res)
							if(res.data.success){
								_this.headimg = res.data.data.headimg;
								_this.id = res.data.data.id;
								_this.nickName = res.data.data.nickName;
								_this.gulijin = res.data.data.gulijin;
								_this.friend = res.data.data.fansCount;
								uni.setStorage({
									key:"gulijin",
									data:res.data.data.gulijin
								})
								uni.setStorage({
									key:"inviteUrl",
									data:res.data.data.inviteUrl
								})
							}
					}
				})
			},
			loginTbao(){
				let isAutoTbao = uni.getStorageSync('isAutoTbao');
				if(!isAutoTbao){
					//授权登录
					//#ifdef APP-PLUS
						plus.runtime.openURL("https://oauth.taobao.com/authorize?response_type=code&client_id=28869937&redirect_uri=https://pandayoupin.com/panda.htm&state=1212&view=wap");
					//#endif
					//#ifdef H5
					window.location.href="https://oauth.taobao.com/authorize?response_type=code&client_id=28869937&redirect_uri=https://pandayoupin.com/panda.htm&state=1212&view=wap";
					//#endif
				}else{
					this.$CommonJS.showToast("您已授权,无需重复授权")
				}
				
			},
			goSharePoster(){
				//专属海报
				this.$CommonJS.navigateTo('../sharePoster/share-poster')
			},
			bindPhone(){
				this.$CommonJS.navigateTo('../user/user-info');
			},
			goEncourageMoney(){
				//我的鼓励金
				this.$CommonJS.navigateTo('../myEncourageMoney/my-encourage-money')
			},
			orderBack(){
				//订单补录
				// this.openOrder();
				this.$CommonJS.navigateTo('../orderBack/order-back')
			},
			goBrowseFootprints(){
				//浏览足迹
				this.$CommonJS.navigateTo('../browseFootprints/browse-footprints')
			},
			goCollection(){
				//收藏夹
				this.$CommonJS.navigateTo('../collection/collection')
			},
			goShopCar(){
				//购物车
				this.$CommonJS.navigateTo('../shopCar/shop-car');
			},
			goNewDriver(){
				this.$CommonJS.navigateTo('../newDriver/newDriver');
			},
			goCash(){
				//提现
				this.$CommonJS.navigateTo('../user/cash-withdrawal');
			},
			goSharePanda(){
				//分享熊猫油品
				this.$CommonJS.navigateTo('../user/share-panda');
			},
			goKeFu(){
				this.$CommonJS.navigateTo('../kefu/kefu');
			},
			
		}
	}
</script>

<style>
	@import url("me.css");
	
</style>
